package com.example.demo.test.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.mybatisplus.TbSysOrganization;
import com.example.demo.test.service.serviceImpl.MpTbSysOrganizationServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xzw
 * @since 2021-01-04
 */
@Slf4j
@Controller
@RequestMapping("/tbSysLogin")
public class MpTbSysLoginController {
	@Autowired
	private MpTbSysOrganizationServiceImpl mpOrganizationServiceImpl;
	
	@RequestMapping(value="/getorglist")
	@ResponseBody
	public Object getOrgList() {
		
		QueryWrapper<TbSysOrganization> wrapper = new QueryWrapper<>();
		
		wrapper.orderByAsc("orgid");
		
		Page page1 = PageHelper.startPage(2, 3);
		
		List<TbSysOrganization> ret = mpOrganizationServiceImpl.list(wrapper);
		
		return ret;
	}
	
	@Transactional
	@ResponseBody
	@RequestMapping(value = "testMysqlLock")
	public void testMysqlLock(String type) {
		log.info("{}尝试获取mysql锁",type);
		mpOrganizationServiceImpl.getOne(
					new LambdaQueryWrapper<TbSysOrganization>().eq(TbSysOrganization::getOrgid, "200000")
					.last(" for update"));
		log.info("{}获取到mysql锁",type);
		if("1".equals(type)) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		log.info("{}释放mysql锁",type);
	}
}

